package me.zingon.WebSocket.tools;

import me.zingon.WebSocket.model.Tank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.socket.WebSocketSession;

/**
 * Created by ZTC on 12/21/2016.
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class TankService {

    Tank tank;

    @Autowired
    Players players;

    public void addPlayer(String name,String reqId,WebSocketSession session){
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        tank= (Tank) wac.getBean("tank");
        tank.setName(name);
        tank.setId(reqId);
        players.addPlayer(reqId,tank,session);
        players.sendScreen();
    }

    public void rmPlayer(String reqId){
        players.rmPlayer(reqId);
    }


    public void doAction(String msg,String reqId) {
        Tank t=players.getAliveTank(reqId);
        if(t==null)
            return;
        t.move(msg);
    }

    public void resA(String id) {
        players.resA(id);
    }

    public void resB(String id){
        players.resB(id);
    }
}
